package com.example.yuejian21.nongfusanquan.yuejian21.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.activity.MotifyActivity;
import com.example.yuejian21.nongfusanquan.yuejian21.activity.MyactivityActivity;
import com.example.yuejian21.nongfusanquan.yuejian21.activity.OrderlistActivity;
import com.example.yuejian21.nongfusanquan.yuejian21.activity.MycollectclubActivity;
import com.example.yuejian21.nongfusanquan.yuejian21.activity.MypostActivity;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Userinfo;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Searchmodel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 皮金 on 2017/9/27.
 */

public class MeFragment extends Fragment {
    private Button setting;
    private View view;
    private TextView me_uname;
    private ImageView me_pic,order,me_collect,me_post,me_act;
    String uid;
    private String mobilenum;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view =inflater.inflate(R.layout.fragment_me, container, false);
        initViews();
//        SharedPreferences sp = getActivity().getSharedPreferences("user", Context.MODE_APPEND);
//        mobilenum = sp.getString("u_phone","0");
        getData(mobilenum);
        initViews();
        getUser_id();
        initEvent();
        return view;

    }
    private void initViews(){
        me_uname = (TextView)view.findViewById(R.id.me_uname);
        me_pic = (ImageView)view.findViewById(R.id.me_pic);
        setting = (Button)view.findViewById(R.id.me_setting);
        order = (ImageView)view.findViewById(R.id.me_order);
        me_collect = (ImageView)view.findViewById(R.id.me_collect);
        me_post = (ImageView)view.findViewById(R.id.me_post);
        me_act = (ImageView)view.findViewById(R.id.me_act);
    }
    private void initEvent(){
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(MeFragment.this.getActivity(), MotifyActivity.class);
                startActivity(intent);

            }
        });

        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(MeFragment.this.getActivity(), OrderlistActivity.class);
                startActivity(intent);
            }
        });
        me_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(MeFragment.this.getActivity(), MycollectclubActivity.class);
                startActivity(intent);
            }
        });

        me_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(MeFragment.this.getActivity(), MypostActivity.class);
                startActivity(intent);
            }
        });
        me_act.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(MeFragment.this.getActivity(), MyactivityActivity.class);
                startActivity(intent);
            }
        });
    }
    private void getData(String user_id) {
        Searchmodel model = new Searchmodel();
        Call<Userinfo> call = model.getUser(user_id);
        call.enqueue(new Callback<Userinfo>() {
            @Override
            public void onResponse(Call<Userinfo> call, Response<Userinfo> response) {
                Userinfo userinfo = response.body();
                me_uname.setText(userinfo.getU_name());

                String upic="http://10.0.2.2/yuejian14/Public/Admin/postphoto/"+ userinfo.getU_pic();
                Picasso.with(MeFragment.this.getActivity()).load(upic).into(me_pic);

            }
            @Override
            public void onFailure(Call<Userinfo> call, Throwable t) {

            }
        });



    }

    public void getUser_id() {
        SharedPreferences sp = getActivity().getSharedPreferences("cache", Context.MODE_APPEND);
        uid = sp.getString("uid","0");

    }


}

